/*
* 提交一级评论
* */
function post(){
    var question_id = $('#question_id').val();
    var content = $('#comment_content').val();
    var commentError = $('#comment_error');
    comment2Target(question_id,1,content,commentError);

}

function comment2Target(targetId, type, content,commentError){
    //判断评论内容是否为空
    if(content == null || '' == content){
        commentError.removeAttr("hidden");
        commentError.text("您回复的评论为空，请输入评论！");
        return ;
    }
    $.ajax({
        type:'POST',
        url:'/comment',
        contentType:'application/json',
        data:JSON.stringify({
            "parentId":targetId,
            "content":content,
            "type":type
        }),
        success:function (response){
            //状态码为200，回复成功
            if(response.code == 200){
                location.reload();
            }else{
                //判断是否登录
                if(response.code == 203){
                    var isAccepted = confirm(response.message);
                    //判断是否确认登录
                    if(isAccepted){
                        window.open("https://gitee.com/oauth/authorize?client_id=970d4ba34d1e302fbc1d09024a1a86e60fb3f8f6da72072ee769360b2ec1d42e&redirect_uri=http://localhost:8081/callback&response_type=code");
                        window.localStorage.setItem("closable",true);
                    }
                }
                window.alert(response.message);
            }
        },
        dataType:'json'
    })
}

//提交二级评论
function comment(e){
    var commentId = e.getAttribute("data-id");
    var content = $('#input-'+commentId).val();
    var commentError = $('#comment_error2'+commentId);
    console.log(commentId);
    comment2Target(commentId,2,content,commentError);
}

/*
* 展开二级评论内容
* */
function collapseComments(e){
    var id = e.getAttribute("data-id");
    var comments = $('#comment-'+id);

    //获取二级评论展开状态
    var collapse = e.getAttribute("data-collapse");
    if(collapse){
        //折叠二级评论
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("comment-active");
    }else{
        console.log(id)
        $.getJSON("/comment/"+id,function (data){
            console.log(data);
            var subCommentContainer = $('#comment-'+id);
            //删除指定class的div元素
            $("div").remove(".comment-del");
            if(data.data != null) {
                $.each(data.data.reverse(), function (i, comment) {
                    var date = new Date(comment.gmtCreate);
                    var date2 = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                    var c = "<div class=\"col-lg-12 col-md-12 col-sm-12 col-xs-12 margin-b10 comment-del comments\"\n" +
                        "                         >\n" +
                        "                        <div class=\"media-left\">\n" +
                        "                            <a href=\"#\">\n" +
                        "                                <img class=\"media-object\" src=\"" + comment.user.avatarUrl + "\" alt=\"...\">\n" +
                        "                            </a>\n" +
                        "                        </div>\n" +
                        "                        <div class=\"media-body\">\n" +
                        "                            <h4 class=\"media-heading\" " + "\">" + comment.user.loginName + "</h4>\n" +
                        "                            <span \">" + comment.commentContent + "</span>\n" +
                        "                        </div>\n" +
                        "                        <div class=\"comment-menu\">\n" +
                        "                            <span class=\"pull-right\"\n" +
                        "                            >" + date2 + "</span>\n" +
                        "                        </div>\n" +
                        "                    </div>";
                    subCommentContainer.prepend(c);

                })
            }
            //添加class in属性，二级评论展开显示
            comments.addClass("in");
            //添加二级评论展开状态
            e.setAttribute("data-collapse","in");
            e.classList.add("comment-active");
        })


    }


}
